package com.wrs.androidkeepalive.utils;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TaskUtils {
    public static  int i = 0;
    static List taskList = new ArrayList();
    static boolean continueThread = true;
    static  Thread thread;
  public  static   void startTask(String tag, Context context, Service service) {
        if (thread != null) {
            taskList.add("");
            continueThread = false;
        } else {
            continueThread = true;
            startThread(tag, context, service);
        }

    }

    static void startThread(String tag, Context context, Service service) {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (continueThread) {

                    try {
                        Thread.sleep(2000);

                        Log.d(tag, "====保活服务===数据支持====");
//                        String pkg = context.getPackageName();
//                        boolean isRuning = AppUtils.isAppRunning(context.getPackageName());
//                        if (isRuning) {
//                            boolean isForeground = AppUtils.isAppForeground(pkg);
//                            if (isForeground) {
//                                Log.d(tag,"====app 在前台运行");
//                            } else {
//                                i ++;
//                                Log.d(tag,"====app 没有在前台运行 i:" + i);
//                            }
//                        } else {
//                            Log.d(tag,"====app 没有在运行中");
//                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                if (taskList.size() > 0) {
                    taskList.removeAll(taskList);
                    startThread(tag, context, service);
                }
            }
        });
        thread.start();

        List list = new ArrayList<String>();
        list.add("");
    }

    public static void startNot(Context context, Service service) {
      NotificationUtils.startFullNot(context, service);
    }

    public static void launchApp(String tag) {
        Log.d(tag,"====launchApp");
        String pkg = "com.wrs.project.jpush";
        AppUtils.launchApp(pkg);
    }

    void timer() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

            }
        };
        //启动定时器 参数对应为 TimerTask 延迟时间 间隔时间
        timer.schedule(task, 2000, 2000);
        timer.cancel();

    }

    void startServicess(Context context, Class<?> cls) {
        context.startService( new Intent(context, cls));
    }


}
